package com.cloud.common.core.exception;


import com.cloud.common.core.enums.ErrorCodeEnum;
import com.cloud.common.core.model.error.ErrorCode;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * API 业务异常类
 * </p>
 */
public class ApiException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    private final ErrorCode errorCode;

    public ApiException(ErrorCodeEnum errorCodeEnum) {
        super(errorCodeEnum.msg());
        this.errorCode = errorCodeEnum.convert();
    }

    public ApiException(ErrorCode errorCode) {
        super(errorCode.getError() + " " + (StringUtils.isNotEmpty(errorCode.getMsg()) ? errorCode.getMsg() : ""));
        this.errorCode = errorCode;

    }

    public ApiException(ErrorCodeEnum errorCodeEnum, Throwable cause) {
        super(errorCodeEnum.msg(), cause);
        this.errorCode = errorCodeEnum.convert();
    }

    public ApiException(ErrorCode errorCode, Throwable cause) {
        super(errorCode.getError() + " " + (StringUtils.isNotEmpty(errorCode.getMsg()) ? errorCode.getMsg() : ""), cause);
        this.errorCode = errorCode;
    }

    public ErrorCode getErrorCode() {
        return errorCode;
    }

}
